package by.msokoloff.javaweb2015.dataaccess.impl;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import by.msokoloff.javaweb2015.dataaccess.TripOrderDao;
import by.msokoloff.javaweb2015.datamodel.TripOrder;
import by.msokoloff.javaweb2015.datamodel.TripOrder_;

@Repository
public class TripOrderDaoImpl extends AbstractDaoImpl<TripOrder> implements TripOrderDao {

	protected TripOrderDaoImpl() {
		super(TripOrder.class);
	}

	@Override
	public TripOrder getDetailed(Integer id) {
		
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<TripOrder> cq = cb.createQuery(TripOrder.class);
		Root<TripOrder> root = cq.from(TripOrder.class);
		cq.where(cb.equal(root.get(TripOrder_.id), id));
		root.fetch(TripOrder_.flights);
		root.fetch(TripOrder_.extras);
		return getEm().createQuery(cq).getResultList().get(0);

	}
	
}